//
//  SYLeftMenu.m
//  MicroNews
//
//  Created by tongcheng on 15/10/20.
//  Copyright © 2015年 siyue. All rights reserved.
//

#import "SYLeftMenuView.h"
#import "SYLeftButton.h"

#define kUserBtuHW 80

@interface SYLeftMenuView()

@property (nonatomic,weak) SYLeftButton *selectedButton;
//存放按钮的数组
@property (nonatomic,strong) NSMutableArray *buttonArr;
//存放>的数组
@property (nonatomic,strong) NSMutableArray *arrowArr;
//背景滚动视图
@property (nonatomic,weak)  UIScrollView *backgroundScroll;

@property (weak, nonatomic) UIView *bottomView;

@end

@implementation SYLeftMenuView

-(NSMutableArray *)buttonArr
{
    if (_buttonArr == nil) {
        _buttonArr = [NSMutableArray array];
    }
    return _buttonArr;
}

-(NSMutableArray *)arrowArr
{
    if(_arrowArr==nil){
        _arrowArr=[NSMutableArray array];
    }
    return _arrowArr;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        [self setupScrollView];
        CGFloat alpha = 1;
        [self setupBtnWithIcon:@"section_latest" title:@"首页" bgColor:SYColorRGBA(243, 243, 243, alpha)];
        
        [self setupBtnWithIcon:@"section_latest" title:@"消息" bgColor:SYColorRGBA(243, 243, 243, alpha)];
        [self setupBtnWithIcon:@"section_latest" title:@"收藏" bgColor:SYColorRGBA(243, 243, 243, alpha)];
        [self setupBtnWithIcon:@"section_latest" title:@"设置" bgColor:SYColorRGBA(243, 243, 243, alpha)];
        [self setupBtnWithIcon:@"section_latest" title:@"设置" bgColor:SYColorRGBA(243, 243, 243, alpha)];
    }
    return self;
}

-(void)setupScrollView
{
    /**
     UIViewContentModeScaleToFill : 图片拉伸至填充整个UIImageView（图片可能会变形）
     
     UIViewContentModeScaleAspectFit : 图片拉伸至完全显示在UIImageView里面为止（图片不会变形）
     
     UIViewContentModeScaleAspectFill :
     图片拉伸至 图片的宽度等于UIImageView的宽度 或者 图片的高度等于UIImageView的高度 为止
     
     UIViewContentModeRedraw : 调用了setNeedsDisplay方法时，就会将图片重新渲染
     
     UIViewContentModeCenter : 居中显示
     UIViewContentModeTop,
     UIViewContentModeBottom,
     UIViewContentModeLeft,
     UIViewContentModeRight,
     UIViewContentModeTopLeft,
     UIViewContentModeTopRight,
     UIViewContentModeBottomLeft,
     UIViewContentModeBottomRight,
     
     经验规律：
     1.凡是带有Scale单词的，图片都会拉伸
     2.凡是带有Aspect单词的，图片都会保持原来的宽高比，图片不会变形
     */

    UIButton *topV = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height/4)];
    [topV addTarget:self action:@selector(userClick:) forControlEvents:UIControlEventTouchDown];//UIControlEventTouchDragInside  UIControlEventTouchDown
    
    UIImageView *userImage = [[UIImageView alloc] init];
    userImage.x = topV.width / 2 - kUserBtuHW/2;
    userImage.y = topV.height /2 -kUserBtuHW/2 -10;
    userImage.height = kUserBtuHW;
    userImage.width = kUserBtuHW;

    userImage.image = [UIImage imageNamed:@"Icon"];
    // 内容模式
    userImage.contentMode = UIViewContentModeScaleAspectFill;
    // 超出边框的内容都剪掉
//    userBtu.clipsToBounds = YES;
//    userBtu.userInteractionEnabled = YES;
    //将图层的边框设置为圆脚
    userImage.layer.cornerRadius = kUserBtuHW/2;
    userImage.layer.masksToBounds = YES;
    //边距
    userImage.layer.borderColor = [UIColor whiteColor].CGColor;
    userImage.layer.borderWidth = 4/[UIScreen mainScreen].scale;
    
    UILabel *userName = [[UILabel alloc] init];
    userName.x = 0;
    userName.y = userImage.y + userImage.height;
    userName.width = topV.width;
    userName.height = kUserBtuHW / 2;
    userName.text = @"小明明";
    userName.textColor = [UIColor whiteColor];
    userName.textAlignment = NSTextAlignmentCenter;
    userName.font = [UIFont boldSystemFontOfSize:17];
//    userName.numberOfLines = 1;
//    [userName sizeToFit];
    //线
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = SYColor(132, 132, 132);
    line.x = 0;
    line.y = topV.height -  1/[UIScreen mainScreen].scale;
    line.width = topV.width;
    line.height =  1/[UIScreen mainScreen].scale;

    [topV addSubview:line];
    [topV addSubview:userName];
    [topV addSubview:userImage];
    [self addSubview:topV];
    
    UIView *bottomV = [[UIView alloc] initWithFrame:CGRectMake(0, self.height/3, self.width, self.height - topV.height)];
    bottomV.backgroundColor = [UIColor whiteColor];
    
    UIScrollView *backgroundScroll = [[UIScrollView alloc] init];
    backgroundScroll.showsHorizontalScrollIndicator = NO;
    backgroundScroll.showsVerticalScrollIndicator  = NO;
    backgroundScroll.contentSize = CGSizeMake(self.width, ScreenHeight+0.5);

    
    
    [self addSubview:backgroundScroll];
    self.bottomView = bottomV;
    self.backgroundScroll = backgroundScroll;

    
//    self.backgroundColor = [UIColor purpleColor];
    topV.backgroundColor = SYSubjectColor;
//    topV.backgroundColor = SYColor(132, 132, 132);
    backgroundScroll.backgroundColor = SYColor(255, 255, 255);
}



/**
 *  添加按钮
 *
 *  @param icon  图标
 *  @param title 标题
 */
- (UIButton *)setupBtnWithIcon:(NSString *)icon title:(NSString *)title bgColor:(UIColor *)bgColor
{
    
    //1。创建按钮
    SYLeftButton *btn = [[SYLeftButton alloc] init];
    btn.tag = self.subviews.count;
    [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    
    [self.backgroundScroll addSubview:btn];
    
    // 设置图片和文字
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"section_latest_highlighted"] forState:UIControlStateSelected];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateSelected];
    [btn setTitleColor:SYColor(132, 132, 132) forState:UIControlStateNormal];//SYColor(132, 132, 132)
    [btn setTitleColor:SYColor(105, 201, 255) forState:UIControlStateSelected];
//    [btn setBackgroundColor:SYRandomColor];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
//    btn.alpha = 0.7;
    // 设置按钮选中的背景
    [btn setBackgroundImage:[UIImage imageWithColor:bgColor] forState:UIControlStateSelected];
    
    // 设置高亮的时候不要让图标变色
//    btn.adjustsImageWhenHighlighted = NO;
    
    // 设置按钮的内容左对齐
    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    
    // 设置间距
    btn.titleEdgeInsets=UIEdgeInsetsMake(0, 20, 0, 0);
    btn.contentEdgeInsets=UIEdgeInsetsMake(0, 30, 0, 0);

    //在按钮右边添加一个>号
    UIImageView *arrow=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Browser_Icon_Forward"]];
    [btn addSubview:arrow];
    [self.arrowArr addObject:arrow];
    
    //把按钮存放到数组中
    [self.buttonArr addObject:btn];

    
        //如果是第一个按钮默认选中
    if(self.buttonArr.count==1){
        [self buttonClick:btn];
    }
    return btn;
}

#pragma mark 按钮的点击事件
-(void)buttonClick:(SYLeftButton *)sender
{
//    NSLog(@"-->方法名--------》%@",sender);
    if([self.delegate respondsToSelector:@selector(leftMenu:didSelectedFrom:to:)]){
        [self.delegate leftMenu:self didSelectedFrom:self.selectedButton.tag to:sender.tag];
    }
    self.selectedButton.selected=NO;
    sender.selected=YES;
//    sender.highlighted=YES;
    self.selectedButton=sender;
}

-(void)userClick:(id)sender
{
    [self.delegate LeftUserClick:(id)sender];
//    NSLog(@"-->方法名--------》%s",__FUNCTION__);
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //1.设置背景滚动视图的frame
    self.backgroundScroll.frame=self.bounds;
    self.backgroundScroll.y = self.height/4;
    //2.设置按钮的frame   按钮上面>的frame
    int btnCount = (int)self.buttonArr.count;
    CGFloat btnW = self.width;
    CGFloat btnH = 0;//self.height / btnCount;
    if(iPhone6 || iPhone6P || iPhone5){
        btnH=60;
    }else{
        btnH=50;
    }
    
    CGFloat arrowW= 15;  //箭头的宽
    CGFloat arrowH=arrowW;   //箭头的高
    for (int i = 0; i<btnCount; i++) {
        SYLeftButton *btn = self.buttonArr[i];
        btn.width = btnW;
        btn.height = btnH;
        btn.y = i * btnH;
        btn.tag=i; //传递tag给button
        
        UIImageView *arrow=self.arrowArr[i];
        CGFloat arrowX=btn.width-arrowW-30;
        CGFloat arrowY=(btn.height-arrowH)*0.5;
        arrow.frame=CGRectMake(arrowX, arrowY, arrowW, arrowH);
    }
}


@end
